/*
*	mgFargo-findUATReason.e 
*	Version 1.00
*
*	This source file is the source of a compiled macro that runs under
*	the Epsilon (see note) text editor.
*
*	Incorporating the macro into Epsilon:
*	-------------------------------------
*	1. Read this file into Epsilon (load it like any other ordinary file)
*	2. Run the command 'compile-buffer'.
*	3. Run the command 'write-state' so that the compiled buffer is made part
*		of your epsilon.sta file and will be automatically available next
*		time epsilon loads.
*
*   Note: "Epsilon" is a registered trademark licensed to Lugaru Software, Ltd. 
*	"EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd.	
*
*	Using the macros:
*	----------------
*	mgFargo_findUATReason.  Using an mgfargo folder, generate a grep folder of e.uat. Probably should delete some.
*
* Also documented in the Wiki at http://development.wiki.hiscox.com/wiki/Magic/Listing_Pending_Code_Changes#Approach_3_-_mgFargo
* and http://development.wiki.hiscox.com/mw/index.php?title=Magic/Identifying_Developer_Work
*	
*	Created:
*		2007-0320 Ira Gershenhorn 
*	
*	Updated:
*        
*/
#include "eel.h"
char insideCall[255];
int mark1;
user char devInitial[] =	"Alison";


struct initialpair{
	char *devName;
	char *devInitials;
} ;

struct initialpair allInitials[20];


/*	  
	  {"Alison","AC"},
	  {"Ira","ILG"}
	"Mark","MAJI",
	"Martin","MV",
	"Neil","NHV",
	"Siby","SJ",
	"Patrick","PK",
	"MarkW","MW",
	"Hendi","HB",
	"Dries","DM",
	"Bennie","BP",
	"Louis","LL",
	"Juan","JW",
	"Silas","SM",
	"XXX","XX"
	  ;
*/
char *getUserInitials( char *devname)
{
	int i;
	i = 0;
	while (1){
		//say("initials [%s] [%s]", allInitials[i].devName, allInitials[i].devInitials);
		if( !strcmp (devname, allInitials[i].devName )){
			//say("initials found for [%s] are [%s]",devname,  allInitials[i].devInitials);
			return allInitials[i].devInitials;
		}
		if( !strcmp ("XXX", allInitials[i].devName )){
			say("initials not found for [%s] at %d",devname, point);
			return "";
		}
		i ++;
	}
}
void grabLine( char *str ){
	to_begin_line();
	do_searching (0,"//");
	grab_line(bufnum, str);
}
// on entry mark1 is at the end of the "e.uat " string
void inCaseWeAreInACall()
{
	int mark2;
	
	insideCall[0]=0;
	to_begin_line();
	mark = point;
	do_searching (0,"e.uat");

	narrow_to_region();
	if( !re_search (RE_FORWARD,"[0-9]%|")){ //if we don't find a match, just return
		widen_buffer();
		return;
	}
	widen_buffer();
	re_search (RE_REVERSE || RE_FIRST_END,"\tCall");
	mark2 = point;
	to_end_line();
	grab( mark2, point, insideCall);
	to_begin_line();
}
command mgFargo_findUATReason()		
{
	char *source;
	char ppmLine[200];
	char devname[100];
	char program[100];
	int grepbufnum;
	int i=0;
	int headerOutput=0;
	char *insideCallPre;
	char *insideCallPost;

	allInitials[i].devName =  "Ira";
	allInitials[i++].devInitials = "ILG";
	allInitials[i].devName =  "Alison";
	allInitials[i++].devInitials = "AC";
	allInitials[i].devName = "Mark";
	allInitials[i++].devInitials = "MAJI";
	allInitials[i].devName = "Martin";
	allInitials[i++].devInitials = "MV";
	allInitials[i].devName = "Neil";
	allInitials[i++].devInitials = "NHV";
	allInitials[i].devName = "Siby";
	allInitials[i++].devInitials = "SJ";
	allInitials[i].devName = "Patrick";
	allInitials[i++].devInitials = "PK";
	allInitials[i].devName = "MarkW";
	allInitials[i++].devInitials = "MW";
	allInitials[i].devName = "Hendi";
	allInitials[i++].devInitials = "HB";
	allInitials[i].devName = "Dries";
	allInitials[i++].devInitials = "DM";
	allInitials[i].devName = "Bennie";
	allInitials[i++].devInitials = "BP";
	allInitials[i].devName = "Louis";
	allInitials[i++].devInitials = "LL";
	allInitials[i].devName = "Juan";
	allInitials[i++].devInitials = "JW";
	allInitials[i].devName = "Silas";
	allInitials[i++].devInitials = "SM";
	allInitials[i].devName = "XXX";
	allInitials[i++].devInitials = "XX";

	if( strcmp(bufname,"grep") ){
		say ("You must start in the grep buffer for this macro to work. This is the %s buffer", bufname);
		return;
	}
	grepbufnum = bufnum;
	while (1){
		char *initials;
		char grepLine[500];

		to_begin_line();
		//say("0: %d", point);
		if( !do_searching( 0, ".txt:") ){
			say( "done - no more lines" );
			break;//if nothing found, then we are done.
		}
		//say("1: %d", point);
		mark1=point;
		grab_line( bufnum, grepLine);
		point=mark1;
		if( !re_search (0, "e.uat ([A-Za-z]+)") ){
			say( "done - no more e.uats" );
			break;//if nothing found, then we are done.
		}
		//say("2: %d", point);
		mark=point;
		//backward_word();
		do_searching( REVERSE," ");
		point++;
		grab( point, mark, devname);
		//say("2a: %d %s", point, devname);
		ppmLine[0] = '\0';	 
		{
			if( strlen(initials = getUserInitials( devname )) == 0 )
				break;

			// Who's e.uat are we finding - do a lookup.
			do_grep_examine(NULL);// dive into the grepped file
			{
				//say("Looking in Context");
				// First look in vicinity
				// remember point, go up a few lines
				// unless its a parameter in a call in which case we
				// should go up a few lines from the call.
				mark1 = point;
				inCaseWeAreInACall();
				nl_reverse();nl_reverse();nl_reverse();nl_reverse();
				mark= mark1;
				narrow_to_region();
				if( !do_searching( 0, initials )){
					//say("Can't find initials [%s]", initials);
				}
				else {
					source = "Context";
					grabLine( ppmLine ); //if found
				}
				widen_buffer();
			}
			// Then look in General Notes
			if( ppmLine[0] == '\0x0') {
				//say("Looking in General Notes");
				point = 1;
				if( !do_searching(0, "Operations: Handler User User General Notes")){
					//say("Can't find General Notes");
				}
				else {
					mark1 = point;
					if( !do_searching (0,"Operations:") ){
						//say("Can't find Operations after General Notes");
					}
					else {
						mark = point;
						point = mark1;
						narrow_to_region();
						if( !do_searching( 0, initials )){
							//say ("can't find initials in General Notes");
						}
						else {
							source = "GenNote";
							grabLine(ppmLine); //if found
						}
					}
				}
				widen_buffer();
			}
			widen_buffer();			
			// Then look from Comment.
			if( ppmLine[0] == '\0x0') {
				//say("Looking in Comments");
				point = 1;
				if( !do_searching( 0, "Commment:" )){
					//say("Can't find comment");
				}
				else {
					mark1 = point;
					if( !do_searching( 0, "Task Levels" )){
						//say("Can't find Task Levels after Comments");
					}
					else {
						mark = point;
						point = mark1;
						narrow_to_region();
						if( !do_searching(0, initials)){
							//say ("can't find initials in Comments");
						}
						else {
							source = "Comment";
							grabLine(ppmLine); //if found
						}
					}
				}
				widen_buffer();
			}
			widen_buffer();
			//say ("Get name of program");
			point = 1;
			if( !do_searching( 0, "Program:" )){
				//say("Can't find Progam");
			}
			else {
				mark1 = point;
				if( !do_searching( 0, "Folder:" )){
					//say("Can't find Folder");
				}
				else {
					do_searching( REVERSE, "Folder:" );//search backwards
					point-=2; //two tabs separate end of program from Folder
					grab( mark1, point, program);
				}
			}
		}
		to_buffer( "x" ); // change to other buffer to store result.
		if( !headerOutput){
			headerOutput = 1;
			bprintf( "%s\t%s\t%s\t%s\t%s\n", "Program", "Developer","Source", "Attribution", "Grep Line" );
		}
		if( ppmLine[0] == '\0x0') {
			source ="n/a";
			sprintf( ppmLine, "n/a");
		}
		if( insideCall[0] == 0 ){
			insideCallPre = "";
			insideCallPost ="";
		}
		else {
			insideCallPre = "Inside ";
			insideCallPost =":";
		}
		bprintf( "%s\t%s\t%s\t%s\t%s%s%s%s\n", program, devname, source, ppmLine, insideCallPre, insideCall, insideCallPost, grepLine );

		to_buffer_num(grepbufnum);//back to starting point
		//say("3: %d", point);
		to_begin_line();
		nl_forward();
		//say("4: %d", point);
	}
	to_buffer( "x" );
	simple_re_replace (0,"\t\t+","\t");//replace multiple tabs with one tab.
}

